const GROUP_NAME = 'answers';
const {
  getAnswers,
  addAnswer,
  getAnswerById,
  deleteAnswerById,
} = require('../handler/answersHandler');

exports.plugin = {
  name: 'routerAnswers',
  register(server) {
    const { Joi, headers } = server;


    server.route([

      { // 获取答卷
        method: 'get',
        path: `/${GROUP_NAME}`,
        handler: getAnswers,
        config: {
          notes: '获取答卷',
          description: '获取答卷',
          tags: ['api', GROUP_NAME],
          validate: {
            headers,
            query: Joi.object({
              paperId: Joi.string().required().hex().length(24)
                .description('问卷id')
                .error(() => new Error('问卷id非法')),
              page: Joi.number().integer().min(1).default(1)
                .description('页码：默认第1页，最小值为1')
                .error(() => new Error('页码：默认第1页，最小值为1')),
              perPage: Joi.number().integer().min(1).default(10)
                .description('每页的记录数：默认10条，最小值为1')
                .error(() => new Error('每页的记录数：默认10条，最小值为1')),
            }),
          },
        },
      },

      { // 添加答卷
        method: 'post',
        path: `/${GROUP_NAME}`,
        handler: addAnswer,
        config: {
          notes: '添加答卷',
          description: '添加答卷',
          tags: ['api', GROUP_NAME],
          // plugins: {
          //   'hapi-swagger': {
          //     payloadType: 'form',
          //   },
          // },
          validate: {
            headers,
            payload: Joi.object({
              paperId: Joi.string().required().hex().length(24)
                .description('问卷id')
                .error(() => new Error('答卷id非法')),
              outlines: Joi.array().items(Joi.object({
                _id: Joi.string().required().hex().length(24),
                answer: Joi.string().required(),
                outlinename: Joi.string().required().min(1).max(50)
                  .trim(),
              }).unknown()).required().description('答卷')
                .error(() => new Error('要搜索的内容')),

              items: Joi.array().items(Joi.object({
                _id: Joi.string().required().hex().length(24),
                itemname: Joi.string().required().min(1).max(50)
                  .trim(),
              }).unknown()).required().description('选项')
                .error(() => new Error('选项必须填写')),
            }),
          },
        },
      },

      { // 根据id获取单个答卷信息
        method: 'GET',
        path: `/${GROUP_NAME}/{id}`,
        handler: getAnswerById,
        config: {
          notes: '根据ID获取单个答卷信息',
          description: '根据ID获取单个答卷信息',
          tags: ['api', GROUP_NAME],
          validate: {
            headers,
            params: Joi.object({
              id: Joi.string().required().hex().length(24)
                .description('答卷id')
                .error(() => new Error('答卷id非法')),
            }),
          },
        },
      },


      { // 根据ID删除答卷
        method: 'delete',
        path: `/${GROUP_NAME}/{id}`,
        handler: deleteAnswerById,
        config: {
          notes: '根据ID删除答卷',
          description: '根据ID删除答卷',
          tags: ['api', GROUP_NAME],
          validate: {
            headers,
            params: Joi.object({
              id: Joi.string().required().hex().length(24)
                .description('答卷id')
                .error(() => new Error('答卷id非法')),
            }),
          },
        },
      },
    ]);
  },
};
